package com.auth.utils.config;

import com.auth.common.result.Result;
import com.auth.common.result.ResultCodeEnum;
import exception.BusinessException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @Author:Tangjiachang
 * @Date 2023/7/13
 * @Description: 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }

    /**
     * @Author Tangjiachang
     * @Description 特定异常处理
     * @Date 22:25 2023/7/20
     * @Param [e]
     * @return com.tang.common.result.Result
     **/
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e) {
        return Result.fail().setMessage(e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result error(BusinessException e) {
        return Result.fail().setCode(e.getCode()).setMessage(e.getMessage());
    }
}
